$(function() {

  });

//switch view/edit mode in my-profile page
$("#customer-profile-update input[type='radio'][name='mode']").click(function(){
	if ($(this).val()=='view'){
		$("form[name='ksr-customer-signup-form']").removeClass("edit");
		$("form[name='ksr-customer-signup-form']").addClass("view");
		$("form[name='ksr-customer-signup-form'] input,form[name='ksr-customer-signup-form'] select,form[name='ksr-customer-signup-form'] textarea").prop("disabled",true);
		$("form[name='ksr-customer-signup-form'] label.error").hide()
	} else {
		$("form[name='ksr-customer-signup-form']").removeClass("view");
		$("form[name='ksr-customer-signup-form']").addClass("edit");
		$("form[name='ksr-customer-signup-form'] label.error").show()
		$("form[name='ksr-customer-signup-form'] input,form[name='ksr-customer-signup-form'] select,form[name='ksr-customer-signup-form'] textarea").prop("disabled",false);
	}
})


	$(".ksr form[name='ksr-customer-signup-form']").each(function(){
		$(this).validate({
			errorContainer:	$(this).find("div.errContainer" ),
			messages:{
				"firstName":"Missing first name.",
				"lastName":"Missing last name.",
				"email":{
					required:"Missing email.",
					email:"Invalid email",
					remote:"This email already exists."
				},
				password:"Missing password.",
				password2:{
					required:"Missing confirmed password.",
					equalTo:"Password mismatch."
				},
				"dateOfBirth":"Missing birthday.",
				"phoneNumber":"Missing phone number.",
				"interest":"Missing interest.",
				"residentAddress.addressLine1":"Missing address",
				"residentAddress.city":"Missing city",
				"residentAddress.state":"Missing state",
				"residentAddress.zip":"Missing zip code"
			},
			submitHandler:function(form){
				if($(form).hasClass("ajax")){//ajax sign up
					console.log("ajax sign up");
					$.post("/ksr/signup.json",$(form).serialize(),"json")
					 .done(function(data){
						 //login successful
						 $(form).parents("div").find("div.errContainer").hide();
						 $(form).parents(".modal.fade").attr("user-login","true");
						 if ($(form).parents(".modal.fade").length>0){
							 $(form).parents(".modal.fade").modal("hide");
						 }
					 })
					 .fail(function(data){
						 console.error("login fail:"+data.responseText);
						 $(form).siblings("div.errContainer").find("label.login-error").html(data.responseText);
						 $(form).siblings("div.errContainer").show("fast");
					 })
					 .always(function(data){
//						 console.log("ajax return value:"+data);
					 })
					return false;
				} else {
					console.log("regular login");
					form.submit();
				}
			}	
		});
	});
  
	$(".ksr form[name='ksr-login-form']").each(function(){
		$(this).validate({
			errorContainer:	$(this).find("div.errContainer" ),
			messages:{
				"email":{
					required:"Missing email.",
					email:"Invalid email"
				},
				password:"Missing password."
			},
			submitHandler:function(form){
				if($(form).hasClass("ajax")){//ajax login
					console.log("ajax login");
					$.post("/ksr/login.json",$(form).serialize(),"json")
					 .done(function(data){
						 //login successful
						 $(form).parents("div").find("div.errContainer").hide();
						 $(form).parents(".modal.fade").attr("user-login","true");
						 if ($(form).parents(".modal.fade").length>0){
							 $(form).parents(".modal.fade").modal("hide");
						 }
					 })
					 .fail(function(data){
						 console.error("login fail:"+data.responseText);
						 $(form).siblings("div.errContainer").find("label.login-error").html(data.responseText);
						 $(form).siblings("div.errContainer").show("fast");
					 })
					 .always(function(data){
//						 console.log("ajax return value:"+data);
					 })
					return false;
				} else {
					console.log("regular login");
					form.submit();
				}
			}	
		});
		
	});
